package com.uuj.project.net;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.bumptech.glide.util.Util;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * ProjectName:    Ok_RxJava_Retrofit_Mvp
 * Package:        com.ok.java.retrofit.net
 * ClassName:      NetRequest
 * Description:    网络请求
 * Author:         张继
 * CreateDate:     2019/4/23 21:07
 * UpdateUser:     更新者
 * UpdateDate:     2019/4/23 21:07
 * UpdateRemark:   更新说明
 * Version:        1.0
 */
public class NetRequest {
    private final static String TAG = NetRequest.class.getSimpleName();
    private static NetRequest mInstance = null;
    private OkHttpClient mClient;
    private Retrofit mRetrofit;
    private static final int mTimeOut = 30;
    private IApiService mService;

    /**
     * 第一步构建单利模式
     */
    public static NetRequest getInstance() {
        if (mInstance == null) {
            synchronized (NetRequest.class) {
                if (mInstance == null) {
                    mInstance = new NetRequest();
                }
            }
        }
        return mInstance;
    }

    /**
     * 第一步：1.获取单例模式时会初始化该类，初始化时就构造好OkHttp和Retrofit两个对象并设置好参数
     */
    private NetRequest() {
        mClient = getClient();
        mRetrofit = getRetrofit();
    }

    /**
     * 第二步 2.初始化OkHttp的参数 并拿到Client请求对象
     */
    private OkHttpClient getClient() {
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String s) {
                LogUtils.eTag(BaseURL.LOG,s);
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClientBuilder.addInterceptor(interceptor);

        //okHttpClientBuilder.hostnameVerifier((hostname, session) -> true);
        //okHttpClientBuilder.cookieJar(new SimpleCookieJar());
        okHttpClientBuilder.connectTimeout(mTimeOut, TimeUnit.SECONDS);
        okHttpClientBuilder.readTimeout(mTimeOut, TimeUnit.SECONDS);
        okHttpClientBuilder.writeTimeout(mTimeOut, TimeUnit.SECONDS);
        okHttpClientBuilder.pingInterval(mTimeOut, TimeUnit.SECONDS);
        okHttpClientBuilder.followRedirects(true);
        //trust all the https point
        //okHttpClientBuilder.sslSocketFactory(HttpsUtils.getSslSocketFactory());
        return okHttpClientBuilder.build();

    }

    /**
     * 第二步 3.初始化Retrofit的参数 并将Client设置给它
     */
    private Retrofit getRetrofit() {
        return new Retrofit.Builder()
                .baseUrl(BaseURL.BASE_URL)// 传入URL
                .client(mClient)// 传入OkHttp的client对象
                .addConverterFactory(GsonConverterFactory.create())// 添加gson解析的支持
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())// 添加支持observable类型
                .build();
    }

    /**
     * 第四步 通过retrofit创建Iapi的实现类对象(retrofit已经帮你实现好了 不需要自己去实现 只要调用方法)
     * 待会model要用来调你写好的Iapi里的接口 接口里定义好了方法为userLogin(放第一步获取的请求体)
     */
    public IApiService getService() {
        if (mService == null) {
            mService = mRetrofit.create(IApiService.class);
        }
        return mService;
    }


}
